<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="4" :lg="7" :md="8" :sm="24">
            <a-form-item label="券名称">
              <a-input placeholder="" v-model="queryParam.actNm" allowClear></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl="4" :lg="7" :md="8" :sm="24">
            <a-form-item label="优惠类型">
              <a-select v-model="queryParam.couponType" allowClear>
                <a-select-option value="mj">满减</a-select-option>
                <a-select-option value="zk">折扣</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :xl="5" :lg="7" :md="8" :sm="24">
            <a-form-item label="有效期">
              <a-range-picker
                :ranges="{ 今天: [moment(), moment()], 本月: [moment().startOf('month'), moment().endOf('month')] }"
                v-model="shelevsDateRange"
                @change="handleDateChange"
              />
            </a-form-item>
          </a-col>
          <a-col :xl="4" :lg="7" :md="8" :sm="24">
            <a-form-item label="适用门店">
              <a-cascader
                :options="departTreeOptions"
                @change="onDepartChange"
                :showSearch="true"
                expand-trigger="hover"
                placeholder=""
                :fieldNames="fieldNames"
                style="width: 180px"
                v-model="queryParam.selecteddeparts"
              />
            </a-form-item>
          </a-col>
          <a-col :xl="3" :lg="7" :md="8" :sm="24">
            <a-form-item label="是否启用">
              <a-select v-model="queryParam.bizStatus" allowClear>
                <a-select-option key="" value="1">是</a-select-option>
                <a-select-option key="" value="0">否</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :xl="4" :lg="7" :md="8" :sm="24">
            <span style="float: left; overflow: hidden" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="default" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="handleAdd" type="primary" icon="plus">新增</a-button>
    </div>

    <!-- table区域-begin -->
    <div>
      <a-table
        ref="table"
        size="middle"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        class="j-table-force-nowrap"
        @change="handleTableChange"
      >
        <span slot="action" slot-scope="text, record">
          <a @click="handleEdit(record)">编辑</a>
        </span>
        <span slot="bizStatus" slot-scope="text, record">
          <a-switch v-model="record.bizStatus" @change="handleStatus(record)" />
        </span>
        <span slot="useDesc" slot-scope="text, record">
          <span :title="text" class="t-ellipsis t-useDesc">{{text}}</span>
        </span>
      </a-table>
    </div>
    <!-- table区域-end -->

    <!-- 表单区域 -->
    <posMarketCouponModal ref="modalForm" @ok="modalFormOk"></posMarketCouponModal>
  </a-card>
</template>

<script>
import '@/assets/less/TableExpand.less'
import posMarketCouponModal from './modules/posMarketCouponModal'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import { postAction } from '@/api/manage'
import moment from 'moment'
import { queryDepartTreeList1 } from '@/api/api'

export default {
  name:'PosMarketCouponList',
  mixins: [JeecgListMixin],
  components: {
    posMarketCouponModal,
  },
  data() {
    return {
      description: '营销活动管理页面',
      disableMixinCreated: true,
      shelevsDateRange: [],
      departTreeOptions: [],
      fieldNames: { label: 'title', value: 'value', children: 'children' },
      moment,
      // 表头
      columns: [
        {
          title: '券编码',
          align: 'center',
          dataIndex: 'actCd',
        },
        {
          title: '券名称',
          align: 'center',
          dataIndex: 'actNm',
        },
        {
          title: '优惠类型',
          align: 'center',
          dataIndex: 'couponType',
          customRender: (text, record) => {
            if(text === 'zk'){
              return '折扣'
            }
            if(text === 'mj'){
              return '满减'
            }
          }
        },
        {
          title: '券有效期',
          align: 'center',
          dataIndex: 'couponValidTime',
        },
        {
          title: '适用门店',
          align: 'center',
          dataIndex: 'scopeShopNames',
          customRender:(text)=>{
            return text + '/全部门店'
          }
        },
        {
          title: '发券总数',
          align: 'center',
          dataIndex: 'distTotal',
        },

        {
          title: '领取数量',
          align: 'center',
          dataIndex: 'getCount',
        },
        {
          title: '剩余数量',
          align: 'center',
          dataIndex: 'remainCount',
          customRender:(text,record)=>{
              let remainCount = 0
              remainCount = record.distTotal - record.getCount
              return remainCount
          }
        },
        {
          title: '是否启用',
          align: 'center',
          dataIndex: 'bizStatus',
          scopedSlots: { customRender: 'bizStatus' },
        },
        {
          title: '使用规则说明',
          align: 'center',
          dataIndex: 'useDesc',
          scopedSlots: { customRender: 'useDesc' },
        },
        {
          title: '操作人',
          align: 'center',
          dataIndex: 'creator',
        },
        {
          title: '操作时间',
          align: 'center',
          dataIndex: 'updateTime',
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          scopedSlots: { customRender: 'action' },
        },
      ],
      url: {
        list: '/marketing/posMarketCoupon/list',
        changeBizStatusUrl: '/marketing/posMarketAct/changeBizStatus',
      },
    }
  },
  created(){
    this.searchQuery()
    this.loadDepartTree()
  },
  methods: {
    searchQuery() {
      let selecteddeparts = this.queryParam.selecteddeparts
      if (selecteddeparts && selecteddeparts.length == 2) {
        this.queryParam.orgScope = selecteddeparts[0]
        this.queryParam.shopScope = selecteddeparts[1]
      }
      this.loadData(1)
    },
    loadDepartTree() {
      queryDepartTreeList1({ orgTypes: '2' }).then((res) => {
        if (res.success) {
          this.departTreeOptions = res.result
          this.departTreeOptions.forEach((x) => {
            x['children'] = [{ title: '全部门店', value: '0' }]
          })
        }
      })
    },
    onDepartChange(value, selectedOptions) {
      console.log('value:', value)
      console.log('selectedOptions:', selectedOptions)
      this.orgIds = value && value.length > 0 ? value[0] : ''
    },
    handleStatus(record) {
      this.loading = true
      let toModify = {}
      toModify.id = record.id
      toModify.bizStatus = record.bizStatus ? 1 : 0
      postAction(this.url.changeBizStatusUrl, toModify)
        .then((res) => {
          if (res.success) {
            this.$message.success('操作成功')
          } else {
            this.$message.warn(res.message)
          }
        })
        .finally(() => (this.loading = false))
    },
    handleDateChange(date, dateString) {
      this.queryParam.startTime = dateString[0]
      this.queryParam.endTime = dateString[1]
    },
  },
}
</script>
<style scoped>
@import '~@assets/less/common.less';

</style>